# -*- coding: utf-8 -*-

from w.blocks.block import BaseBlock as Block
from w.blocks.containers.edit_box import EditBox as EditBoxContainer

class Error(Exception):
    pass

class EditBox(EditBoxContainer, Block):
    scriptFiles = ['w/blocks/edit_box.js']

    def __init__(self, content=None, disableDefaultValue=False,
                 focusedClass=None, changedClass=None, *argv, **kwargv):
        EditBoxContainer.__init__(self, content, *argv, **kwargv)
        Block.__init__(self, *argv, **kwargv)

        self.tag.html.id = 'dom%s'%self.blockID
        self.disableDefaultValue = disableDefaultValue
        self.focusedClass = focusedClass
        self.changedClass = changedClass

        if not self.disableDefaultValue:
            self.tag.html.onFocus=self.js.onFocusIn()
            self.tag.html.onBlur=self.js.onFocusOut()

    def jsCode(self):
        if self.disableDefaultValue:
            return Block.jsCode(self)
        return '%s\n%s\n%s\n%s'%(Block.jsCode(self),
                            self.js.defaultValue << self[0],
                            self.js.changeClassOnFocusTo << self.focusedClass,
                            self.js.changeClassOnValueTo << self.changedClass)

    def make(self, content=[]):
        return Block.make(self, EditBoxContainer.make(self, content))

